package com.yonyougov.devops.cruxcisupport.roleenv;

import com.yonyougov.devops.cruxcisupport.roleenv.dto.AddMulRoleEnvRequestDto;
import com.yonyougov.devops.cruxcisupport.roleenv.dto.GetEnvIdsByRoleIdsReqDto;
import com.yonyougov.devops.cruxcisupport.roleenv.dto.RoleEnvDeleteBatchRequestDto;
import com.yonyougov.devops.cruxcisupport.roleenv.dto.RoleEnvPageByRoleIdRequestDto;
import com.yonyougov.devops.cruxcisupport.roleenv.vo.FindEnvIdsByRoleIdsDto;
import com.yonyougov.devops.cruxcisupport.roleenv.vo.RoleEnvPageByRoleIdDto;
import com.yonyougov.devops.cruxcisupport.roleenv.vo.RoleEnvPageByRoleIdVo;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.core.controller.CrudController;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigInteger;
import java.util.List;

/**
 * Created by ligson on 2017/8/11.
 */
@Api(value = "角色关联环境模块", tags = "角色关联环境模块")
@RestController
@RequestMapping("/api/v2/roleenv")
public class RoleEnvController extends CrudController<RoleEnvEntity, BigInteger> {

    private final RoleEnvService roleEnvService;

    public RoleEnvController(RoleEnvService roleEnvService) {
        this.roleEnvService = roleEnvService;
    }

    @Override
    public BaseService<RoleEnvEntity, BigInteger> getService() {
        return roleEnvService;
    }

    //<!--用户自定义代码开始-->

    @ApiOperation(value = "角色管理模块关联环境调用", notes = "批量新增关联")
    @PostMapping(value = "/re/add/batch")
    public BaseResponseDto addMulRoleEnv(@RequestBody @Valid AddMulRoleEnvRequestDto request) {
        AddMulRoleEnvRequestDto addMulRoleEnvRequestDto = new AddMulRoleEnvRequestDto();
        BeanUtils.copyProperties(request, addMulRoleEnvRequestDto);

        roleEnvService.addMulRoleEnv(addMulRoleEnvRequestDto);

        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "角色管理模块关联环境调用", notes = "批量删除关联")
    @PostMapping(value = "/re/delete/batch/ids")
    @SneakyThrows
    public BaseResponseDto DeleteBatchRoleEnvByIds(@RequestBody @Valid RoleEnvDeleteBatchRequestDto request) {
        roleEnvService.deleteMulRoleEnvByPrimaryKey(request.getIds());
        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "角色管理模块关联环境调用", notes = "查询一个角色的关联关系（分页）（可根据环境名过滤）（分页）")
    @PostMapping(value = "/re/page/like")
    public BaseResponseDto findRoleEnvPageByRoleId(@RequestBody @Valid RoleEnvPageByRoleIdRequestDto request) {
        RoleEnvPageByRoleIdDto roleEnvPageByRoleIdDto = new RoleEnvPageByRoleIdDto();
        BeanUtils.copyProperties(request, roleEnvPageByRoleIdDto);

        Pagination<RoleEnvPageByRoleIdVo> pagination = roleEnvService.findRoleEnvPageByRoleId(roleEnvPageByRoleIdDto);

        BaseResponseDto baseResponseDto = BaseResponseDto.getSuccessInstance();
        baseResponseDto.put("totalCount", pagination.getTotalCount());
        baseResponseDto.put("data", pagination.getDatas());
        return baseResponseDto;
    }

    @ApiOperation(value = "前端部署模块调用", notes = "根据roleIds查询envIds（去重）")
    @PostMapping(value = "/re/envids/roleids")
    public BaseResponseDto findRoleEnvPageByRoleId(@RequestBody @Valid GetEnvIdsByRoleIdsReqDto request) {
        FindEnvIdsByRoleIdsDto getEnvIdsByRoleIdsReqVo = new FindEnvIdsByRoleIdsDto();
        BeanUtils.copyProperties(request, getEnvIdsByRoleIdsReqVo);

        List<BigInteger> datas = roleEnvService.getEnvIdsByRoleIds(getEnvIdsByRoleIdsReqVo);

        BaseResponseDto baseResponseDto = BaseResponseDto.getSuccessInstance();
        baseResponseDto.put("data", datas);
        return baseResponseDto;
    }

    //<!--用户自定义代码结束-->
}
